﻿using System;
using System.Windows;
using MyFloridaHouse.ServiceConn.DistrBldrSvcRef;
using MyFloridaHouse.libSvOpenDistricts.Criteria;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace MyFloridaHouse.libSvOpenDistricts.DataService
{
    public class PendingGetRegionsDataInViewRequest
    {
        public int layerID;
        public ObservableCollection<RegionDTO> regionList;

        public PendingGetRegionsDataInViewRequest(int layerID, ObservableCollection<RegionDTO> regionList)
        {
            this.layerID = layerID;
            this.regionList = regionList;
        }
    }

    public delegate void ServiceDALGetLayerRegionsDataInViewCompletedEventHandler(LayerRegionsDataSTO args);

    public partial class ServiceDAL
    {
        public static event ServiceDALGetLayerRegionsDataInViewCompletedEventHandler GetLayerRegionsDataInViewCompleted;


        private static void RaiseGetLayerRegionsDataInViewCompleted(object sender, LayerRegionsDataSTO sto)
        {
            if (GetLayerRegionsDataInViewCompleted != null)
            {
                GetLayerRegionsDataInViewCompleted(sto);
            }
            else
            {
                // todo: Log this error
                throw (new NotImplementedException("No Subscribers to GetLayerRegionsDataInViewCompleted Event"));
            }
        }

        private int _layerRegionsDataInViewRequestActiveCount = 0;
        public int layerRegionsDataInViewRequestActiveCount
        {
            set
            {
                if (value != _layerRegionsDataInViewRequestActiveCount)
                {
                    _layerRegionsDataInViewRequestActiveCount = value;
                    NotifyPropertyChanged("layerRegionsDataInViewRequestActiveCount");
                }
            }
            get { return _layerRegionsDataInViewRequestActiveCount; }
        }


        private PendingGetRegionsDataInViewRequest pendingGetRegionsDataInViewRequest = null;

        public void GetLayerRegionsDataInView( int layerID, ObservableCollection<RegionDTO> regionList)
        {
            try
            {
                if (layerRegionsDataInViewRequestActiveCount > 0)
                {
                    // set this incoming request as the next request, forget about a prior request
                    pendingGetRegionsDataInViewRequest = new PendingGetRegionsDataInViewRequest(layerID, regionList);
                }
                else
                {
                    DataDictionaryIDList dictionaryIDstruct = CriteriaDatasetManager.GetActiveDictionaryIDs();
                    List<string> newRregionIDList = new List<string>();
                    foreach (RegionDTO rgn in regionList)
                    {
                        newRregionIDList.Add(rgn.regionID.ToString());
                    }

                    string regionIDList = string.Join(",", newRregionIDList);

                    var proxy = new DistrBldrClient(_relativeBindingName);
                    proxy.GetLayerRegionsDataInViewCompleted += ServiceGetLayerRegionsDataInViewCompleted;
                    proxy.GetLayerRegionsDataInViewAsync(layerID, regionIDList, dictionaryIDstruct.layerIDList, dictionaryIDstruct.dictionaryIDList);
                    layerRegionsDataInViewRequestActiveCount++;
                }
            }
            catch (Exception ex)
            {
                layerRegionsDataInViewRequestActiveCount--;
                MessageBox.Show(ex.Message);
            }

        }

        private void ServiceGetLayerRegionsDataInViewCompleted(object sender, GetLayerRegionsDataInViewCompletedEventArgs args)
        {
            layerRegionsDataInViewRequestActiveCount--;
            try
            {
                if (!IsError("GetLayerRegionsDataInViewCompleted", args.Error, args.Result, args.Result.errorMessage))
                {
                    RaiseGetLayerRegionsDataInViewCompleted(this, args.Result);
                    if (pendingGetRegionsDataInViewRequest != null)
                    {
                        GetLayerRegionsDataInView(pendingGetRegionsDataInViewRequest.layerID, pendingGetRegionsDataInViewRequest.regionList);
                        pendingGetRegionsDataInViewRequest = null;
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
